import Intra from "./page/Intra";
import Domain from "./str/Domain";
import StrTo from "./utils/StrTo";
import Module from "./str/Module";
import Unit from "./str/Unit";

/**
 * 字符处理
 */



export default class CharNode{

constructor(swithcDomain,wordTypeId) {

    this.charDomain=swithcDomain;
    this.wordTypeId=wordTypeId;
}

    /**
     * 切割字符
     * 查找字符id
     */
    run(wordStr){
        let resModule=new Module();
        if(wordStr.length==0){
            return resModule;
        }

        let wordModule=this.charDomain.find(this.wordTypeId);

        let srts=wordStr.split("");

        srts.forEach(str=>{
            let  strNum=StrTo.utf8ToNum(str)
            let wordId="";

            let unit ;
            let rowList= wordModule.isRows("","",strNum,this.wordTypeId)
            if(rowList.length>0){
                unit = rowList[0].find(1);
            }else {
                unit= new Unit(wordId,"",strNum,this.wordTypeId);
            }
            resModule.addUnit(unit);
        })


        return resModule;
    }
    /**
     * 添加字
     */
    addChar(wordStr){
        let resIds=[];
        if(wordStr.length==0){
            return resIds;
        }

        let wordModule=this.charDomain.find(this.wordTypeId);

        let srts=wordStr.split("");

        srts.forEach(str=>{
            let  strNum=StrTo.utf8ToNum(str)


            let rowList= wordModule.isUnits("","",strNum,this.wordTypeId)
            if(rowList.length<=0){
                this.charDomain.addUnit( "","",strNum,this.wordTypeId) ;
            }
        })


        return resIds;
    }


    loadData( intras){
        this.charDomain=new Domain();

        this.charDomain.loadData(intras)
    }


    outData(){

        let intras = this.charDomain.outData();

        return intras
    }

}
